package com.fishery.memo.config;


import com.fishery.memo.entity.Task;
import com.fishery.memo.service.TaskService;
import com.fishery.memo.util.MessageManager;
import org.quartz.Scheduler;
import org.springframework.beans.BeansException;
import org.springframework.boot.CommandLineRunner;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author run
 * @since 2021/1/31 11:13
 */
@Service
public class InitConfig implements CommandLineRunner, ApplicationContextAware {

    private ApplicationContext applicationContext;

    /**
     * spring容器初始化后将数据库的任务全部注册。
     */
    @Override
    public void run(String... args) {
        TaskService taskService = applicationContext.getBean(TaskService.class);
        MessageManager messageManager = applicationContext.getBean(MessageManager.class);
        List<Task> runningTask = taskService.searchRunningTask();
        runningTask.forEach((task -> {
            try {
                messageManager.registerMessage(task);
            } catch (Exception e) {
                e.printStackTrace();
            }
        }));

    }

    @Override
    public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
        this.applicationContext = applicationContext;
    }
}
